<?php

include (dirname(__FILE__) . '/classes/FirePHP.php');
require (dirname(__FILE__) . '/config/config.inc.php');
require (dirname(__FILE__) . '/classes/Db.php');
require (dirname(__FILE__) . '/classes/Archievement.class.php');

if ($_SERVER["REQUEST_METHOD"] == "POST") {

    session_start();
    $db = new DB();

    if (isset($_POST['createArchievement'])) {

        $archievement = new Archievement('0', $_POST['name'], $_POST['description'], '5', $_SESSION['awesome_id'], 1, 0, time(), time(), time(), 1);

        $result = $db->createArchievement($archievement);
        if ($result) {
            $firebug = FirePHP::getInstance(true);
            //$firebug->log(json_encode(htmlspecialchars($archievement->printArchievement())));
//               $firebug->log($archievement->printArchievement());
            // $response = '{ "response":"success", "archievement": [] }';
            $response = $archievement->printArchievement();
            // if ($count >= 1) {
            // $response .= ', "achievements" : [';
            // }
            // while ($row = mysql_fetch_assoc($result)) {
            // $response .= json_encode($row) . ",";
            // }
            // $response = substr($response, 0, -1);
            // $response .= '] }';
            // SELECT a.* FROM user_achievement AS ua
            // JOIN achievement AS a ON ua.id_archievement = a.id WHERE ua.id_user = 1
            // SELECT * FROM pre_achievement
            // JOIN pre_achievement_lang AS lang ON pre_achievement.id_message  = lang.id
            // JOIN pre_category AS category ON pre_achievement.id_category = category.id
            // JOIN pre_category_lang AS category_lang ON category_lang.id_category = category.id WHERE category_lang .id_lang = 2 AND lang.id_lang = 2

            echo $response;
            return TRUE;
        } else {
            echo $response = '{ "response":"fail", "message": "=("}';
            return FALSE;
        }
    }
    if (isset($_POST['finishAchievement'])) {
                $firebug = FirePHP::getInstance(true);
          $result = $db->finishArchievement($_POST['idAchievement']);
//        if ($result) {
//            
//        }
    }
}